package com.gtw.modules.sys.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.annotation.SysLog;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbCategoryService;
import com.gtw.modules.entity.TbCategory;
import com.gtw.modules.entity.TbUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 一级分类表
 *
 * @author wuye
 * @email www.baidu.com
 * @date 2018-01-26 10:37:03
 */
@RestController
@RequestMapping("sys/category")
public class CategoryController {

    @Autowired
    private TbCategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:category:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<TbCategory> pageUtil = new Page<TbCategory>(query.getPage(), query.getLimit());
        Page<TbCategory> page = categoryService.queryPageList(pageUtil, query);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:category:info")
    public R info(@PathVariable("id") Integer id) {
        TbCategory category = categoryService.selectByPrimaryKey(id);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @SysLog("添加")
    @RequestMapping("/save")
    @RequiresPermissions("sys:category:save")
    public R save(@RequestBody TbCategory category) {
        category.setCreatedate(new Date());
        category.setIsdelete(false);
        categoryService.insertSelective(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改")
    @RequestMapping("/update")
    @RequiresPermissions("sys:category:update")
    public R update(@RequestBody TbCategory category) {
        categoryService.updateByPrimaryKey(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:category:delete")
    public R delete(@RequestBody Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            categoryService.deleteByPrimaryKey(ids[i]);
        }

        return R.ok();
    }

}
